<template>
  <div class="app-container">
    <el-form size="mini" :inline="true" :model="searchForm" @keyup.enter.native="getDataList()">
      <el-form-item prop="name">
        <el-input v-model="searchForm.name" placeholder="可输入用户名、手机号进行搜索" clearable style="width: 300px"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="mini" @click="getDataList()" type="primary">查询</el-button>
        <el-button size="mini" @click="refDataList()" type="primary">刷新</el-button>
        <el-button size="mini" @click="addOrUpdateHandle()" type="primary" v-if="Jurisdictionlist.indexOf('Add') > -1">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="dataList" size="mini" border v-loading="dataListLoading" @selection-change="selectionChangeHandle" style="width: 100%">
      <el-table-column prop="typeName" header-align="center" width="80" align="center" label="编号">
        <template slot-scope="scope">
          <span>{{ scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="name" header-align="center" align="center" label="用户名"></el-table-column>
      <el-table-column prop="phone" header-align="center" align="center" label="手机号"></el-table-column>
      <el-table-column prop="create_user" header-align="center" align="center" label="姓名"></el-table-column>
      <el-table-column prop="email" header-align="center" align="center" label="绑定邮箱"></el-table-column>
      <el-table-column prop="is_email" header-align="center" align="center" label="是否绑定">
        <template slot-scope="scope">
          {{ scope.row.is_email == 1 ? '已绑定' : '未绑定' }}
        </template>
      </el-table-column>
      <el-table-column prop="roles__name" header-align="center" align="center" label="角色名称"></el-table-column>
      <el-table-column prop="status" header-align="center" align="center" label="状态">
        <template slot-scope="scope">
          {{ scope.row.status == 1 ? '激活' : '冻结' }}
        </template>
      </el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="200" label="操作">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="addOrUpdateHandle(scope.row)" v-if="Jurisdictionlist.indexOf('Edit') > -1">编辑</el-button>
          <el-button size="mini" type="text" @click="handleDel(scope.row)" v-if="Jurisdictionlist.indexOf('Delete') > -1">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination small style="margin-top: 10px" @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex" :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage" layout="total, sizes, prev, pager, next, jumper"></el-pagination>

    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update ref="addOrUpdate" @refreshList="getDataList"></add-or-update>
    <!-- 删除前通过发送验证码检验身份，此为验证码弹框 -->
    <!-- 忘记密码找回弹框 -->
    <el-dialog title="校验身份" :visible.sync="dialogFormVisible">
      <el-form size="mini" ref="dataForm" :rules="rules" :model="dataForm" status-icon label-position="left">
        <!-- <el-form-item label="手 机 号 ： " prop="phone">
          <el-input ref="phone"  style="width:60%" v-model="dataForm.phone" />
        </el-form-item> -->
        <el-form-item label="验 证 码 ： " prop="code">
          <el-input style="width: 40%" v-model="dataForm.code" />
          <span class="getCode" v-if="!isNext" @click="getcode">获取验证码</span>
          <span class="getCode" v-else-if="isNext && time > 0">{{ time }}秒后重发</span>
          <span class="getCode" v-else @click="sendYanzheng">重新发送</span>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="qxyzm">取消</el-button>
        <el-button size="mini" type="primary" @click="modifypass">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import AddOrUpdate from './add-or-update'
import { userView, userRemove } from '@/api/system/user.js'
import { validUsername, isPassword, isMobile } from '@/utils/validate'
import { sendmsg, resetpassword } from '@/api/login.js'
export default {
  data() {
    return {
      delrow: [], // 删除行信息
      dialogFormVisible: false, // 修改密码弹框控制
      timer: undefined, // 倒计时全局删除
      isNext: false,
      time: 90, // 发送短信时间,倒计时90s
      rules: {
        phone: [{ required: true, message: '请输入手机号', trigger: 'blur' }],
        code: [{ required: true, message: '请输入验证码', trigger: 'blur' }],
      },
      dataForm: {
        code: undefined,
      },
      Jurisdictionlist: [], // 权限列表
      searchForm: {
        name: '',
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      roleOptions: [],
    }
  },
  components: { AddOrUpdate },
  mounted() {
    this.getDataList()
    // 处理权限
    this.Jurisdictionlist = JSON.parse(localStorage.getItem('jurisdiction'))[this.$route.path] || []
  },
  computed: {},
  methods: {
    qxyzm() {
      this.dialogFormVisible = false
      this.time = 90
      this.isNext = false
      clearInterval(this.timer)
      this.$refs.dataForm.resetFields()
    },
    getcode() {
      this.sendYanzheng()
    },
    sendYanzheng() {
      var that = this
      sendmsg({ phone: that.$store.state.user.phone }).then((res) => {
        if (res.code == 200) {
          this.$notify.success({
            title: '成功',
            message: '验证码发送成功请注意查收',
          })
          this.isNext = true
          this.time = 90
          this.timer = setInterval(() => {
            if (that.time > 0) {
              this.time -= 1
            } else {
              clearInterval(timer)
            }
          }, 1000)
        }
      })
    },
    finaldel(row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning',
      })
        .then(() => {
          let para = {
            ids: [row.id],
            code: this.dataForm.code,
          }
          userRemove(para).then((res) => {
            if (res.code == 200) {
              this.$message.success('删除成功')
              this.getDataList()
            }
          })
        })
        .catch(() => {})
    },
    modifypass() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          var that = this
          let para = {
            ids: [this.delrow.id],
            code: this.dataForm.code,
          }
          userRemove(para).then((res) => {
            if (res.code == 200) {
              this.$message.success('删除成功')
              this.qxyzm()
              this.getDataList()
            }
          })
        }
      })
    },
    //重置
    refDataList() {
      this.searchForm.name = ''
      this.getDataList()
    },
    handleDel(row) {
      this.dialogFormVisible = true
      this.delrow = row
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      const info = Object.assign({ pageNumber: this.pageIndex, pageSize: this.pageSize }, this.searchForm)
      userView(info)
        .then((res) => {
          this.dataList = res.data
          this.totalPage = res.total
          this.roleOptions = res.others.roles
          this.dataListLoading = false
        })
        .catch((e) => {
          this.dataList = []
          this.totalPage = 0
          this.dataListLoading = false
        })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {},
    // 新增 / 修改
    addOrUpdateHandle(row) {
      this.$refs.addOrUpdate.open(row, this.roleOptions)
    },
  },
}
</script>

<style>
.my-btn .el-form-item__content {
  width: 100%;
}
</style>
<style lang="scss" scoped>
.getCode {
  width: 100px;
  height: 40px;
  line-height: 40px;
  text-align: center;
  display: inline-block;
  color: #fff;
  background-color: #409eff;
  cursor: pointer;
  box-sizing: border-box;
  font-size: 14px;
  border-radius: 4px;
  margin-left: 20px;
}
</style>
